package com.heima.article.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.file.service.FileStorageService;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.article.service.GeneratePageService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class GeneratePageServiceImpl implements GeneratePageService {
    @Autowired
    private Configuration configuration;
    @Resource(name = "minIOFileStorageService")
    private FileStorageService fileStorageService;
    @Value("${file.oss.prefix}")
    private String prefix;
    @Autowired
    ApArticleContentMapper apArticleContentMapper;
    @Autowired
    ApArticleMapper apArticleMapper;
    @Autowired
    ApAuthorMapper apAuthorMapper;

    /**
     * 根据文章id生成文章静态页面
     * @param articleId
     */

   // @Override
    @Async
    public void generateArticlePage(long articleId) {
        //获取文章内容

            try {
                ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery()
                        .eq(ApArticleContent::getArticleId, articleId));
                if (apArticleContent != null && StringUtils.isNotBlank(apArticleContent.getContent())) {
                    ApArticle apArticle = apArticleMapper.selectById(articleId);
                    ApAuthor apAuthor = apAuthorMapper.selectById(apArticle.getAuthorId());
                    Template template = null;
                    template = configuration.getTemplate("article.ftl");
                    //封装数据
                    Map<String, Object> params = new HashMap<>();
                    params.put("article", apArticle);
                    params.put("content", JSONArray.parseArray(apArticleContent.getContent()));
                    params.put("authorApUserId", apAuthor.getUserId());
                    StringWriter stringWriter = new StringWriter();
                    template.process(params, stringWriter);
                    ByteArrayInputStream ins = new ByteArrayInputStream(stringWriter.toString().getBytes());
                    //4.生成页面把html文件上传到minio中
                    String path = fileStorageService.store(prefix, apArticleContent.getArticleId() + ".html", "html/text", ins);


                    //5.修改ap_article表，保存static_url字段
                    apArticle.setStaticUrl(path);
                    apArticleMapper.updateById(apArticle);

                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("静态页面生成失败======>articleId:{}",articleId,e.getCause());
            }
    }
}
